import * as api from '../../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    business_array: [],
    business_obj: [],
    business_index: 0,
    business_mobile:'',
    showModal: false,
    active: 1,
    activeList: {
      0: {
        name: '全部',
        value: 'all'
      },
      1: {
        name: '待接单',
        value: '0'
      },
      2: {
        name: '待安装',
        value: '1'
      },
      3: {
        name: '已完成',
        value: '2'
      },
    },
    list: {},
  },
  onChange(event) {
    const that = this
    const active = event.detail.name
    const status = that.data.activeList[active].value
    this.setData({
      active
    })
    that.orderList(status)
  },
  orderList(status = '0') {
    const that = this
    let list = this.data.list
    const childList = list && list[status] ? list[status] : null
    const current_page = childList ? childList.current_page : 1
    const last_page = childList ? childList.last_page : 1
    if (childList && current_page >= last_page) {
      console.log('No more data')
      return false
    }
    const page = childList && current_page < last_page ? current_page + 1 : 1
    api.customerInstallOrderList(status, page)
      .then(res => {
        if (childList && current_page < res.data.current_page) {
          list[status]['data'] = childList.data.concat(res.data.data)
          list[status]['current_page'] = res.data.current_page
        } else {
          list[status] = res.data
        }
        that.setData({
          ['list.' + status]: list[status]
        })
      }).catch(err => console.log(err))
  },
  nextorderdetails: function (e) {
    const order_sn = e.currentTarget.dataset.sn
    wx.navigateTo({
      url: '/pages/order/installOrder/details?order_sn=' + order_sn,
    })
  },
  getBusinessList(){
    let app = this
    let cityId = wx.getStorageSync('cityId')
    api.getBusinessList(cityId)
      .then(res => {
        let business_array = []
        let business_obj = res.data
        business_obj.forEach(element => {
          business_array.push(element.nickname)
        });
        let business_mobile = ''
        if(business_obj.length > 0) {
          business_mobile = business_obj[0].mobile
        }
        app.setData({
          business_array,business_obj,business_mobile
        })
      }).catch(err => console.log(err))
  },
  // 弹出层
  relation() {
    let app = this
    if(app.data.business_mobile == '') {
      wx.showToast({
        title: '所在地区暂无可联系商家',
        icon: 'none',
        image: '',
        duration: 2000
      });
      return false
    }
    this.setData({
      showModal: true
    })
  },
  go: function () {
    this.setData({
      showModal: false
    })
  },
  // 关闭弹窗
  cancel: function () {
    this.setData({
      showModal: false
    })
  },
  // 关闭弹窗
  contact: function () {
    let app = this
    wx.makePhoneCall({
      phoneNumber: app.data.business_mobile
    })
    app.setData({
      showModal: false
    })

  },
  bindBusinessChange: function(e) {
    let business_index = e.detail.value
    let business_obj = this.data.business_obj
    let business_mobile = business_obj[business_index].mobile
    this.setData({
      business_index,business_mobile
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.orderList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getBusinessList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this
    const active = that.data.active
    const status = that.data.activeList[active].value
    that.orderList(status)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})